package com.probiz.estore.product.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.common.model.product.ProductTypeSkuOption;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.product.dao.ProductTypeSkuOptionDao;

/**
 * Dao implementation for ProductTypeSkuOption.
*/
public class ProductTypeSkuOptionDaoImpl extends HibernateGenericDaoImpl<ProductTypeSkuOption> implements ProductTypeSkuOptionDao {

	@SuppressWarnings("unchecked")
	public List<ProductType> findActiveSkuOptionsProductType() {
		String hql="SELECT DISTINCT ptso.productType FROM ProductTypeSkuOption ptso where ptso.skuOption.status=?";
		List<ProductType>productTypes=findByHql(hql,Constants.STATUS_ACTIVE);
		return productTypes;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.dao.ProductTypeSkuOptionDao#getBySkuOptionIdAndBrandId(java.lang.Integer, java.lang.Integer)
	 */
	public ProductTypeSkuOption getByProductTypeIdAndSkuOptionId(Integer productTypeId, Integer skuOptionId) {
		List<ProductTypeSkuOption> list = this.findByHql("from ProductTypeSkuOption ps where ps.productType.productTypeId =? and ps.skuOption.skuOptionId=?", productTypeId,skuOptionId);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.dao.ProductTypeSkuOptionDao#saveProductTypeSkuOptionsFromArray(com.probiz.estore.common.model.product.ProductType, java.lang.String[], java.lang.String[])
	 */
	public List<String> saveProductTypeSkuOptionsFromArray(ProductType productType,	String[] skuOptionIds, String[] skuOptionIsQuickSearchs){
		List<String> newSkuOptionIdList = new ArrayList<String>();
		//删除关联规格
		List<String> brandIdList= Arrays.asList(skuOptionIds);
		Set<ProductTypeSkuOption> formerProductTypeSkuOptionList=productType.getProductTypeSkuOptions();//原来的关联
		for (ProductTypeSkuOption productTypeSkuOption : formerProductTypeSkuOptionList) {
			if(!brandIdList.contains(productTypeSkuOption.getSkuOption().toString())){
				delete(productTypeSkuOption);
			}
		}
		//增加或者修改关联规格
		for (int i = 0; i < skuOptionIds.length; i++) {
			ProductTypeSkuOption productTypeSkuOption;
			productTypeSkuOption = getByProductTypeIdAndSkuOptionId(productType.getProductTypeId(),Integer.valueOf(skuOptionIds[i]));
			if(null == productTypeSkuOption){
				//case:新增
				productTypeSkuOption = new ProductTypeSkuOption();
			}
			productTypeSkuOption.setProductType(productType);
			productTypeSkuOption.setSkuOptionId(new Integer(skuOptionIds[i]));
			productTypeSkuOption.setIsQuickSearch(new Integer(skuOptionIsQuickSearchs[i]));
			productTypeSkuOption.setSortOrder(i);
			save(productTypeSkuOption);
			newSkuOptionIdList.add(productTypeSkuOption.getProductTypeSkuOptionId().toString());
		}
		return newSkuOptionIdList;
	}
}
